<template>
  <div class="">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1 form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="brName">
          <a-input v-model:value="formState.brName" class="w-64" placeholder="客户姓名/助记码/手机号" />
        </a-form-item>
        <a-form-item name="dateRange" label="预约日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" :allow-clear="false" value-format="YYYY-MM-DD" @change="data" />
        </a-form-item>
        <a-form-item name="empName" label="预约医师">
          <a-input v-model:value="formState.empName" class="w-64" placeholder="医师" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex">
        <a-button class="mb-2 mr-2" type="primary" @click="onCLickEdit"> 添加 </a-button>
        <BaseExport
          type="理疗"
          name="预约管理"
          code="00001143"
          class=""
          :search-params="{ startTime: formState.dateRange?.[0], endTime: formState.dateRange?.[1], ...formState }"
          >导出</BaseExport
        >
      </div>
      <BaseTable
        :data-source="tableData"
        fill
        :expanded-row-keys="expandedRowKeys"
        :loading="loading"
        column-code="00001143"
        :pagination="pagination"
        @expand="onShowDetail"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'lsh'">
            <PopoverTable :config="detailConfig" :width="600" :params="{ lsh: record.lsh }">{{ record.lsh }}</PopoverTable>
          </template>
          <template v-if="column.dataIndex === 'yysj'">
            {{ record.yyrq + ' ' + record.yysj }}
          </template>
          <template v-if="column.dataIndex === 'statusStr'">
            <a-tag :color="statusEnum[record.status].color">{{ statusEnum[record.status].text }}</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <BaseButton type="link" size="small" :disabled="record.status !== 0" @click="onCLickEdit(record)"> 修改 </BaseButton>

              <BaseButton confirm-text="确认取消？" type="link" danger size="small" :disabled="record.status !== 0" @click="onCLickStrap(record)">
                取消
              </BaseButton>
              <BaseButton type="link" size="small" :disabled="record.status !== 0" @click="onCLickCheckin(record)"> 登记 </BaseButton>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuccess" />
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import useStore from '../store'
import Edit from './Edit'

const store = useStore()
const router = useRouter()

const editOpen = ref(false)
const rowData = ref({})
const formRef = ref(null)
const statusEnum = [
  { text: '有效', color: 'green' },
  { text: '到场', color: 'blue' },
  { text: '取消', color: 'red' },
  { text: '过期', color: 'red' }
]

const detailConfig = ref({
  url: '/course/serviceItemAssociation/getCourseAppointment',
  columnCode: '00001179',
  rowKey: 'id',
  formatResult: (data) => {
    return data.courseAppointmentDetailList.map((item, index) => ({
      ...item,
      xh: index + 1
    }))
  }
})

const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs(new Date().setDate(new Date().getDate() + 10)).format('YYYY-MM-DD')]
})
const onSuccess = () => {
  getData()
}
const onCLickCheckin = (record) => {
  store.currentRegistrants = record
  router.push({
    path: '/physiotherapy/check-in'
    // query: {
    //   id: record.id
    // }
  })
}
const onCLickStrap = async (record) => {
  const res = await post('/course/serviceItemAssociation/cancelCourseAppointment', {
    lsh: record.lsh
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}

const { loading, tableData, getData, pagination } = useTable({
  url: '/course/serviceItemAssociation/getCourseAppointmentList',
  immediate: true,
  formatParams: () => {
    return {
      startTime: formState.value.dateRange?.[0],
      endTime: formState.value.dateRange?.[1],
      ...formState.value
    }
  }
})
const onCLickEdit = (record = {}) => {
  rowData.value = record
  editOpen.value = true
}

onMounted(() => {
  getData()
})
</script>
